package com.smart.controller;

import com.smart.common.WebResponse;
import com.smart.common.annotation.WebAccess;
import com.smart.common.PageInfo;
import com.smart.model.request.QrCodeRequest;
import com.smart.service.QrCodeService;
import com.smart.model.vo.QrCodeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * @author zhanghong
 */
@RestController
@RequestMapping("/code")
@Api(value = "进出场二维码模块", tags = {"进出场二维码模块"})
public class QrCodeController {

    @Autowired
    private QrCodeService qrCodeService;

    @ApiOperation(value = "根据主键查询", httpMethod = "POST")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "success")})
    @PostMapping("/get/{id}")
    public WebResponse<QrCodeVO> getByKey(HttpServletRequest request, @PathVariable("id") long id) {
        return WebResponse.build(qrCodeService.getByKey(id));
    }

    @WebAccess(login = true)
    @ApiOperation("分页查询")
    @PostMapping("/pageQuery")
    public WebResponse<PageInfo<QrCodeVO>> pageQuery(@RequestBody QrCodeRequest request) {
        return WebResponse.build(qrCodeService.pageQuery(request));
    }

    @WebAccess(login = true)
    @ApiOperation("列表查询")
    @PostMapping("/listQuery")
    public WebResponse<List<QrCodeVO>> listQuery(@RequestBody QrCodeRequest request) {
        return WebResponse.build(qrCodeService.listQuery(request));
    }

    @WebAccess(login = true)
    @ApiOperation("入场离场二维码生成")
    @PostMapping("/qrCode")
    public WebResponse qrCode(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam("address") String address,
                       @RequestParam("addressType")Boolean addressType){
        return new WebResponse<>(qrCodeService.qrCode(request,response, false, null, address,addressType));
    }

    @WebAccess(login = true)
    @ApiOperation("修改")
    @PostMapping("/update")
    public WebResponse<Boolean> updateByKey(@Valid @RequestBody QrCodeRequest request) {
        return WebResponse.build(qrCodeService.updateByKey(request));
    }

    @WebAccess(login = true)
    @ApiOperation("删除")
    @PostMapping("/delete/{id}")
    public WebResponse<Boolean> deleteByKey(HttpServletRequest request, @PathVariable("id") long id) {
        return WebResponse.build(qrCodeService.deleteByKey(id));
    }
}
